<?php

/**
 * RabbitCMS
 */
class Menu_Models_Menu extends Rabbit_Db_Table_Abstract implements Menu_Models_Interfaces_Menu {

    protected $_name = 'menu';

    public function convert($entity) {
        return new Menu_Models_Entities_Menu($entity);
    }

    public function getById($id) {
        $row = $this->getAdapter()
                        ->select()
                        ->from(array('m' => $this->_name))
                        ->where('m.id = ?', $id)
                        ->limit(1)
                        ->query()
                        ->fetch();
        return (null == $row) ? null : new Menu_Models_Entities_Menu($row);
    }

    public function add($menu) {
        $this->getAdapter()->insert($this->_name,
                array(
                    'name' => $menu->name,
                    'description' => $menu->description,
                    'user_id' => $menu->user_id,
                    'user_name' => $menu->user_name,
                    'created_date' => $menu->created_date,
                    /**
                     * @since 2.0.8
                     */
                    'language' => $menu->language
                    
        ));
        return $this->getAdapter()->lastInsertId($this->_name);
    }

    public function update($menu) {
        return $this->getAdapter()->update($this->_name,
                array(
                    'name' => $menu->name,
                    'description' => $menu->description,
                    /**
                     * @since 2.0.8
                     */
                    'language' => $menu->language,
                ),
                array(
                    'id = ?' => $menu->id,
        ));
    }

    public function delete($id) {
        $where['id = ?'] = $id;

        /**
         * @since 2.0.7
         */
        $this->getAdapter()->delete($this->_prefix . 'menu_item', array('menu_id =? ' => $id));

        return $this->etAdapter()->delete($this->_name, $where);
    }

    public function getMenus($offset = null, $count = null) {
        $select = $this->getAdapter()
                        ->select()
                        ->from(array('m' => $this->_name))
                        /**
                         * @since 2.0.8
                         */
                        ->where('m.language = ?', $this->_lang)
                        ->order('m.id DESC');
        if (is_int($offset) && is_int($count)) {
            $select->limit($count, $offset);
        }
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function count() {
        return $this->getAdapter()
                ->select()
                ->from(array('m' => $this->_name), array('num_menus' => 'COUNT(*)'))
                /**
                 * @since 2.0.8
                 */
                ->where('m.language = ?', $this->_lang)
                ->limit(1)
                ->query()
                ->fetch()
        ->num_menus;
    }

    /* ========== For translation =========================================== */

    public function getTranslations($item) {
        $rs = $this->getAdapter()
                        ->select()
                        ->from(array('m' => $this->_name))
                        ->joinInner(array('tr' => $this->_prefix . 'core_translation'),
                                'tr.item_class = ?
						AND (tr.item_id = ? OR tr.source_item_id = ?)
						AND (tr.item_id = m.id OR tr.source_item_id = m.id)',
                                array('tr.source_item_id'))
                        ->group('m.id')
                        ->bind(array(
                            'Menu_Models_Entities_Menu',
                            $item->id,
                            $item->id,
                        ))
                        ->query()
                        ->fetchAll();
        return (null == $rs) ? null : new Tomato_Model_RecordSet($rs, $this);
    }

    public function getTranslatable($lang) {
        $rs = $this->getAdapter()
                        ->select()
                        ->from(array('m' => $this->_name))
                        ->joinLeft(array('tr' => $this->_prefix . 'core_translation'),
                                'tr.source_item_id = m.id
							AND tr.item_class = ? 
							AND tr.language = ?',
                                array('translatable' => '(tr.item_id IS NULL)'))
                        ->where('m.language = ?')
                        ->bind(array('Menu_Models_Entities_Menu', $lang, $this->_lang))
                        ->query()
                        ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function getSource($menu) {
        $row = $this->getAdapter()
                        ->select()
                        ->from(array('m' => $this->_name))
                        ->joinInner(array('tr' => $this->_prefix . 'core_translation'), 'm.id = tr.source_item_id', array())
                        ->where('tr.item_id = ?', $menu->id)
                        ->where('tr.item_class = ?', 'Menu_Models_Entities_Menu')
                        ->limit(1)
                        ->query()
                        ->fetch();
        return (null == $row) ? null : new Menu_Models_Entities_Menu($row);
    }

}
